﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Zj.VueSample.Api.Data.Core;
using Zj.VueSample.Api.Models;

namespace Zj.VueSample.Api.Controllers
{
    [Produces("application/json")]
    [Route("api/Products/[action]")]
    public class ProductsController : Controller
    {
        private readonly List<Product> _products;
        private readonly List<ProdComment> _comments;
        public ProductsController()
        {
            _products = new List<Product>();
            _comments = new List<ProdComment>();
            for (int i = 1; i <= 6; i++)
            {
                _products.Add(new Product()
                {
                    Category = new string[] { "图书", "电子信息", "科技" },
                    ProdDesc = $"这是描述{i}",
                    Id = i,
                    ProdPrice = 25 * 0 + 12,
                    ProdName = $"这是标题{i}",
                    ProdImg = "http://placehold.it/320x150"
                });
                _comments.Add(new ProdComment()
                {
                    Id = i,
                    Content = "不错，五星好评！",
                    Date = DateTime.Now,
                    UserName = "gainorloss",
                    Rating = 5
                });
            }
        }
        [HttpGet]
        public IActionResult GetAll()
        {
            return Json(_products);
        }
        [HttpGet]
        public IActionResult GetProductById(int id)
        {
            return Json(_products.SingleOrDefault(p => p.Id == id));
        }
        [HttpGet]
        public IActionResult GetCommentsByProductId(int id)
        {
            return Json(_comments);
        }
        public IActionResult GetProductsBySearchParams(ProductSeachParamsInputDto inputDto)
        {
            var rt = _products;
            if (!string.IsNullOrEmpty(inputDto.Title))
            {
                rt = rt.Where(p => p.ProdName.Contains(inputDto.Title)).ToList();
            }
            if (!string.IsNullOrEmpty(inputDto.Category) && !inputDto.Category.Equals("-1"))
            {
                rt = rt.Where(p => p.Category.Contains(inputDto.Category)).ToList();
            }
            if (inputDto.Price > 0)
            {
                rt = rt.Where(p => p.ProdPrice <= inputDto.Price).ToList();
            }
            return Json(rt);
        }
    }
}